package com.paw.hys.sa.client.fallback.factory;

import com.paw.hys.sa.FallbackAlertService;
import com.paw.hys.sa.client.ServiceHbClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lvzihai
 * @date 2022/9/15
 **/
@Slf4j
@Component
public class ServiceHbClientFallbackFactory implements FallbackFactory<ServiceHbClient> {

  @Autowired
  private FallbackAlertService fallbackAlertService;

  @Override
  public ServiceHbClient create (Throwable e) {
    return new ServiceHbClient(){
      @Override
      public String callHc () {

//        log.error("ServiceHbClientFallbackFactory error",e);
        String traceId = ThreadContext.get("traceId");
        log.info("traceId={}",traceId);
        String klassName = this.getClass().getName();
        String simpleClassName = this.getClass().getSimpleName();
        String className = this.getClass().getInterfaces()[0].getName();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        String klassMethod = className +"#" + methodName;
        // hello world
        //className: com.hello.demo.TestA, simpleClassName: TestA, methodName: hello
        log.error("fallback method= {}" , klassMethod, e);
        fallbackAlertService.alert(this.getClass(),e);
        return "ServiceHbClientFallbackFactory callHc";
      }
    };

  }
}
